package com.extract.query;

import com.eksframework.commons.core.base.EksDate;
import com.extract.sys.entity.TaskRecord;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;

/**
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-10-15 15:53
 */
public class QueryParameter {

    private static final QueryParameter INSTANCE = new QueryParameter();
    private static final Map<String, String> PARAMETER_MAP = Maps.newHashMap();

    private QueryParameter(){
    }

    public static QueryParameter of(){
        return INSTANCE;
    }

    public static QueryParameter of(TaskRecord taskRecord){
        QueryParameter param = new QueryParameter();
        if(Objects.isNull(taskRecord)){
            return param;
        }
        param.put("tenantId", taskRecord.getTenantId());
        param.put("cinemaCode", taskRecord.getCinemaCode());
        param.put("systemCode", taskRecord.getSystemCode());
        param.put("lastSign", taskRecord.getLastSign());
        param.put("startTime", taskRecord.getExecTimeStart());
        param.put("endTime", taskRecord.getExecTimeEnd());
        param.put("limit", taskRecord.getIntervalNum());
        return param;
    }

    public QueryParameter put(final String key, final String value){
        if(Objects.isNull(key) || Objects.isNull(value)){
            return this;
        }
        PARAMETER_MAP.put(StringUtils.trim(key), StringUtils.trim(value));
        return this;
    }

    public QueryParameter put(final String key, final int value){
        return put(key, String.valueOf(value));
    }

    public QueryParameter put(final String key, final long value){
        return put(key, String.valueOf(value));
    }

    public QueryParameter put(final String key, final Long value){
        if (Objects.isNull(key) || Objects.isNull(value)) {
            return this;
        }
        return put(key, value.toString());
    }

    public QueryParameter put(final String key, final LocalDateTime value){
        if (Objects.isNull(key) || Objects.isNull(value)) {
            return this;
        }
        return put(key, EksDate.of(value).toDateTimeString());
    }

    public Map<String, String> getParameterMap(){
        return PARAMETER_MAP;
    }
}
